package com.kpijavatraining.module;
/**
 * Created with IntelliJ IDEA.
 * User: Yura
 * Date: 13.10.13
 * Time: 14:32
 * To change this template use File | Settings | File Templates.
 */

import java.io.IOException;

import com.kpijavatraining.module.io.ConsolePrinter;
import com.kpijavatraining.module.io.ConsoleReader;
import com.kpijavatraining.module.io.SystemSave;

/**
 * We use this class to asking questions
 *
 */
public class Teacher
{
	private ConsoleReader consoleReader;
	private ConsolePrinter consolePrinter;
   
    public Teacher()  throws IOException
    {
    	consoleReader = new ConsoleReader();
		consolePrinter = new ConsolePrinter();
    }

    /**
     * Controller
     * @throws java.io.IOException
     */
    public void performExam(Session session) throws IOException
    {
    	while(session.getNumberOfQuestion() < session.getCountOfQuestions()){
			
    		consolePrinter.println("1 - go to the next question");
            consolePrinter.println("2 - go to the prev question");
            consolePrinter.println("3 - save this session and quit");
			switch(consoleReader.readLn()){
				case "1" : nextQuestion(session);break;
				case "2" : prevQuestion(session);break;
				case "3" : save(session); return;
				default  : consolePrinter.println("������� ��������");
			}
		}
    }

	public void close(){
		consoleReader.close();
	}
	
	private void prevQuestion(Session session){
		session.prevQuestion();
	}
	
	private void nextQuestion(Session session){
		consolePrinter.print(session.toString());
		session.answer(consoleReader.readLn());
	}
	
	private void save(Session session) throws IOException{
		new SystemSave().save(session);
	}
}
